﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

public class PauseEnv : EnvBase
{
    // Consts
    private static int kQuitOption      = 495;
    private static int kResumeOption    = 435;
    private static int kArrowWidth      = 290;
    private static int kArrowHeight     = 59;

    // Member vars
    private Texture2D m_gameScreen;
    private Animation m_pauseScreen;
    private Animation m_arrows;
    private bool m_resumeOption;

    /**
     * Our constructor.
     * Grabs the previous Env as a texture for drawing over.
     */
    public PauseEnv()
    {
        m_gameScreen = Game.getGame().getEnvStack().currentEnvAsTexture();
        m_resumeOption = true;
        loadAssets();
    }

    /**
     * Load up our graphics.
     */
    private void loadAssets()
    {
        m_pauseScreen = new Animation("pauseMenu", 0, false);
        m_pauseScreen.addFrame(
            new Frame(
                Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.GUI).get("pauseMenu"),
                new Rectangle(0,0,Game.kScreenWidth,Game.kScreenHeight),
                0,
                new Vector2(0, 0)
                ));

        m_arrows = new Animation("arrows", 0, false);
        m_arrows.addFrame(
            new Frame(
                Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MENU).get("mainScreenArrows"),
                new Rectangle(0, 0, kArrowWidth, kArrowHeight),
                0,
                Vector2.Zero
                ));
    }

    /**
     * Our update. Tests for resume/quit calls.
     */
    override public void update(double dt)
    {
        InputManager im = InputManager.getSingleton();
        im.update();
        if (im.getJustPressedControlState(InputManager.PAUSE_BUTTON) ||
            im.getJustPressedControlState(InputManager.JUMP_BUTTON))
        {
            Game.getGame().getSoundManager().playOneSoundEffect(((AudioAsset)Game.getGame().getAssetLibrary().getAudio(AssetLibrary.audioType.SOUND_EFFECT).get("menu_select")).sound, 1.0f);
            if (m_resumeOption)
            {
                m_parentStack.popOnce();
            }
            else
            {
                m_parentStack.popAll(new MenuBase());
            }
        }
        if (im.getJustPressedControlState(InputManager.UP_BUTTON) ||
            im.getJustPressedControlState(InputManager.DOWN_BUTTON) )
        {
            m_resumeOption = !m_resumeOption;
            Game.getGame().getSoundManager().playOneSoundEffect(((AudioAsset)Game.getGame().getAssetLibrary().getAudio(AssetLibrary.audioType.SOUND_EFFECT).get("menu_click")).sound, 1.0f);
        }
    }

    /**
     * Our render. Draws the previous Env underneath our menu options.
     */
    public override void render()
    {
        Game.getGame().getSpriteBatch().Draw(m_gameScreen, new Vector2(0,0), null, Color.White, 0.0f,
            new Vector2(0,0), 1.0f, SpriteEffects.None, Priority.kHUDFar);
        m_pauseScreen.render(Vector2.Zero, Priority.kConsoleFar);

        if (m_resumeOption)
        {
            m_arrows.render(new Vector2((Game.kScreenWidth - kArrowWidth) / 2, kResumeOption), Priority.kConsoleNear);
        }
        else
        {
            m_arrows.render(new Vector2((Game.kScreenWidth - kArrowWidth) / 2, kQuitOption), Priority.kConsoleNear);
        }
    }
}
